// https://www.acwing.com/problem/content/837/

#include <iostream>
#include <string>

using namespace std;

// 这个解法是在 2 的基础上做了空间优化，即结构体中 children 放弃使用指针而是使用索引。

struct TrieNode {
    int children[26] = {};
    int cnt = 0;
};

const int MAX_ARRAY_SIZE = 1e5 + 5;
TrieNode nodes[MAX_ARRAY_SIZE];
int idx = 0;

void Insert(const string& word) {
    auto p = 0;
    for (const char ch: word) {
        int t = ch - 'a';
        if (!nodes[p].children[t]) {
            nodes[p].children[t] = ++idx;
        }
        p = nodes[p].children[t];
    }
    nodes[p].cnt++;
}

int Query(const string& word) {
    auto p = 0;
    for (const char ch: word) {
        int t = ch - 'a';
        if (!nodes[p].children[t]) {
            return 0;
        }
        p = nodes[p].children[t];
    }
    return nodes[p].cnt;
}

int main() {
    int n;
    char ch;
    string word;
    cin>>n;
    for (int i=0; i<n; i++) {
        cin>>ch>>word;
        if (ch == 'I') {
            Insert(word);
        } else if (ch == 'Q') {
            cout<<Query(word)<<endl;
        }
    }
    return 0;
}
